const {
    createshuoshuo,
    uploadshuoshuoimgname,
    getshuoshuoByTime,
    gettopshuoshuo,
    checkShuoshuo,
    edit,
    updatashuoshuoimglist,
    updatacovers,
    gettopunm,
    updatashuoshuoTop,
    updatashuoshuostatus,
    deleteshuoshuo,
    setlikeshuoshuo
} = require("../serve/shuoshuo")
const {
    getUserByID,
} = require("../serve/users.js")
const sequelize = require("../model/db.js")
const Response = require("../util/respanse.js")
const { updataname } = require("../util/qiniuUpload.js")
/* 编辑说说 */
const editshuoshuo = async (req, res) => {
    const t = await sequelize.transaction();
    try {
        let { shuoshuo_author, shuoshuo_authorid, shuoshuo_id,
            shuoshuo_content, shuoshuo_cover, shuoshuo_istop, shuoshuo_status } = req.body
        let ress = await gettopshuoshuo(false, shuoshuo_id);
        if (ress.code) {
            /* 先查看上传的用户是否存在 且 权限是否为最高权限 */
            shuoshuo_authorid = typeof shuoshuo_authorid == "string" ? parseInt(shuoshuo_authorid) : shuoshuo_authorid
            let isuser = await getUserByID(shuoshuo_authorid)
            /* 匹配权限 和 用户名 */
            if (isuser.code == 1 && isuser.users.Users_role == 1 && shuoshuo_author == isuser.users.Users_name) {
                let result = await edit({ shuoshuo_author, shuoshuo_authorid, shuoshuo_content, shuoshuo_cover, shuoshuo_istop, shuoshuo_status, shuoshuo_id })
                console.log(result.msg);
                if (result.code) {
                    await t.commit();
                    Response.successresponse(res, {
                        code: 200,
                        message: "添加成功",
                        data: { shuoshuo_id: result.data.shuoshuo_id },
                    })
                } else {
                    await t.rollback();
                    Response.errorresponse(res, {
                        code: 400,
                        message: "添加失败"
                    })
                }
            }
        } else {
            await t.rollback();
            Response.errorresponse(res, {
                code: 400,
                message: "修改失败"
            })
        }

    } catch (error) {
        await t.rollback();
        console.log(error);
        Response.errorresponse(res, {
            msg: "修改失败",
            code: 500,
            err: error
        })
    }
}
/* 添加说说 */
const addshuoshuo = async (req, res) => {
    const t = await sequelize.transaction();
    try {

        let { shuoshuo_author, shuoshuo_authorid,
            shuoshuo_content, shuoshuo_cover, shuoshuo_istop, shuoshuo_status } = req.body
        /* 先查看上传的用户是否存在 且 权限是否为最高权限 */
        shuoshuo_authorid = typeof shuoshuo_authorid == "string" ? parseInt(shuoshuo_authorid) : shuoshuo_authorid
        let isuser = await getUserByID(shuoshuo_authorid)
        /* 匹配权限 和 用户名 */
        if (isuser.code == 1 && isuser.users.Users_role == 1 && shuoshuo_author == isuser.users.Users_name) {
            let result = await createshuoshuo({ shuoshuo_author, shuoshuo_authorid, shuoshuo_content, shuoshuo_cover, shuoshuo_istop, shuoshuo_status })
            console.log(result.msg);
            if (result.code) {
                await t.commit();
                Response.successresponse(res, {
                    code: 200,
                    message: "添加成功",
                    data: {
                        shuoshuo_id: result.data.id
                    }
                })
            } else {
                await t.rollback();
                Response.errorresponse(res, {
                    code: 400,
                    message: "添加失败"
                })
            }
        }

    } catch (error) {
        await t.rollback();
        console.log(error);
        Response.errorresponse(res, {
            msg: "添加失败",
            code: 500,
            err: error
        })
    }
}
/* 上传说说的图片 */
const uploadshuoshuoimg = async (req, res) => {
    try {
        let { shuoshuo_id, shuoshuo_imglist } = req.body
        let ress = await uploadshuoshuoimgname(shuoshuo_id, shuoshuo_imglist);
        console.log(ress.msg);
        if (ress.code) {
            Response.successresponse(res, {
                code: 200,
                message: "上传成功",
            })
        } else {
            Response.errorresponse(res, {
                code: 400,
                msg: "上传失败"
            })
        }
    } catch (error) {
        console.log(error);
        Response.errorresponse(res, {
            code: 500,
            msg: "上传失败",
            err: error
        })
    }
}
/* 分页获取说说 */
const getshuoshuo = async (req, res) => {
    try {
        let { page, pageSize, shuoshuo_status } = req.body;
        let ress = await getshuoshuoByTime(page, pageSize, shuoshuo_status);
        console.log(ress.msg);
        if (ress.code) {
            Response.successresponse(res, {
                code: 200,
                message: "获取成功",
                data: ress.data
            })
        } else {
            Response.errorresponse(res, {
                code: 400,
                msg: ress.msg
            })
        }
    } catch (error) {
        console.log(error);
        Response.errorresponse(res, {
            code: 500,
            msg: "获取失败",
            err: error
        })
    }
}
/* 获取置顶说所 */
const gettop = async (req, res) => {
    try {
        let ress = await gettopshuoshuo(true);
        if (ress.code) {
            Response.successresponse(res, {
                code: 200,
                message: "获取成功",
                data: ress.data
            })
        } else {
            Response.successresponse(res, {
                code: 402,
                message: ress.msg
            })
        }
    } catch (error) {
        console.log(error);
        Response.errorresponse(res, {
            code: 500,
            msg: "出错了"
        })
    }
}
/* 获取说说 */
const getshuoshuoByid = async (req, res) => {
    try {
        let { id } = req.query;
        id = (typeof id == "string") ? parseInt(id) : id
        let ress = await gettopshuoshuo(false, id);
        console.log(ress.msg);
        if (ress.code) {
            Response.successresponse(res, {
                code: 200,
                message: "获取成功",
                data: ress.data
            })
        } else {
            Response.successresponse(res, {
                code: 400,
                message: ress.msg
            })
        }
    } catch (error) {
        console.log(error);
        Response.errorresponse(res, {
            code: 500,
            msg: "出错了"
        })
    }
}
/* 修改说说的图片 */
const updatashuoshuoimg = async (req, res) => {
    console.log(req.body);
    let { shuoshuo_id, shuoshuo_imglist } = req.body
    try {
        let ress = await gettopshuoshuo(false, shuoshuo_id);
        console.log(ress.msg);
        if (ress.code) {
            let data = await updatashuoshuoimglist(ress.data.id, shuoshuo_imglist)
            if (data.code) {
                Response.successresponse(res, {
                    code: 200,
                    message: "获取成功",
                })
            }
        }
    } catch (error) {
        console.log(error);
        Response.errorresponse(res, {
            code: 500,
            msg: "出错了",
            err: error
        })
    }
}
/* 修改封面 */
const updatacover = async (req, res) => {
    try {
        console.log(req.body);
        let { shuoshuo_id, shuoshuo_cover, oldshuoshuocover } = req.body
        updataname(oldshuoshuocover, shuoshuo_cover, async function (
            err, respBody, respInfo) {
            if (err) {
                console.log(err);
            } else {
                if (respInfo.statusCode == 200) {
                    let ress = await updatacovers(shuoshuo_id, shuoshuo_cover, oldshuoshuocover)
                    console.log(ress);
                    if (ress.code) {
                        Response.successresponse(res, {
                            code: 200,
                            message: "修改成功"
                        })
                    } else {
                        Response.successresponse(res, {
                            code: 200,
                            message: "修改成功"
                        })
                    }
                }
            }
        })
    } catch (error) {
        console.log(error);
        Response.errorresponse(res, {
            code: 500,
            msg: "出错了",
            err: error
        })
    }
}
/* 查询能否置顶 */
const cantop = async (req, res) => {
    try {
        let ress = await gettopunm()
        if (ress) {
            Response.errorresponse(res, {
                code: 400,
                msg: "不能置顶"
            })
        } else {
            Response.successresponse(res, {
                code: 200,
                message: "可以置顶"
            })
        }
    } catch (error) {
        console.log(error);
        Response.errorresponse(res, {
            code: 500,
            msg: "出错了",
            err: error
        })
    }
}
/* 说说置顶 和取消*/
const updatatop = async (req, res) => {
    try {
        let { shuoshuo_id, istotop } = req.body
        if (istotop) {
            let ress = await gettopunm()
            if (ress) {
                Response.errorresponse(res, {
                    code: 400,
                    msg: "不能置顶"
                })
            } else {
                let data = await updatashuoshuoTop(shuoshuo_id, true)
                if (data.code) {
                    Response.successresponse(res, {
                        code: 200,
                        message: "置顶成功"
                    })
                } else {
                    Response.errorresponse(res, {
                        code: 400,
                        msg: "置顶失败",
                    })
                }
            }
        } else {
            /* 取消置顶 查看当前说说是否是置顶*/
            let ress = await gettopshuoshuo(false, shuoshuo_id)
            if (ress.code) {
                let istop = ress.data.shuoshuo_istop
                if (istop) {
                    let data = await updatashuoshuoTop(shuoshuo_id, false)
                    if (data.code) {
                        Response.successresponse(res, {
                            code: 200,
                            message: "取消置顶成功"
                        })
                    } else {
                        Response.errorresponse(res, {
                            code: 400,
                            msg: "取消置顶失败",
                        })
                    }
                } else {
                    Response.errorresponse(res, {
                        code: 400,
                        msg: "已经取消置顶"
                    })
                }
            } else {
                Response.errorresponse(res, {
                    code: 400,
                    msg: "取消置顶失败"
                })
            }

        }
    } catch (error) {
        console.log(error);
        Response.errorresponse(res, {
            code: 500,
            err: error,
            msg: "出错了"
        })
    }
}
/* 修改是否公开 */
const tovisible = async (req, res) => {
    try {
        let { shuoshuo_id, invisible } = req.body
        if (invisible) {
            /* 隐藏 */
            let ress = await gettopshuoshuo(false, shuoshuo_id)
            if (ress.code) {
                let status = ress.data.shuoshuo_status
                if (status == 1) {
                    let data = await updatashuoshuostatus(shuoshuo_id, 2)
                    if (data.code) {
                        Response.successresponse(res, {
                            code: 200,
                            message: "隐藏成功"
                        })
                    } else {
                        Response.errorresponse(res, {
                            code: 400,
                            msg: "隐藏失败",
                        })
                    }
                } else {
                    Response.errorresponse(res, {
                        code: 400,
                        msg: "说说已经隐藏"
                    })
                }
            } else {
                Response.errorresponse(res, {
                    code: 400,
                    msg: "修改失败"
                })
            }
        } else {
            /* 公开 */
            let ress = await gettopshuoshuo(false, shuoshuo_id)
            if (ress.code) {
                let status = ress.data.shuoshuo_status
                if (status == 2) {
                    let data = await updatashuoshuostatus(shuoshuo_id, 1)
                    if (data.code) {
                        Response.successresponse(res, {
                            code: 200,
                            message: "公开成功"
                        })
                    } else {
                        Response.errorresponse(res, {
                            code: 400,
                            msg: "公开失败",
                        })
                    }
                } else {
                    Response.errorresponse(res, {
                        code: 400,
                        msg: "说说已经公开"
                    })
                }
            } else {
                Response.errorresponse(res, {
                    code: 400,
                    msg: "修改失败"
                })
            }
        }
    } catch (error) {
        console.log(error);
        Response.errorresponse(res, {
            code: 500,
            msg: "服务器错误",
            err: error
        })
    }
}
/* 删除说说 */
const todelete = async (req, res) => {
    try {
        let { shuoshuo_id } = req.body
        let ress = await gettopshuoshuo(false, shuoshuo_id)
        if (ress.code) {
            let deleted = await deleteshuoshuo(shuoshuo_id)
            if (deleted.code) {
                Response.successresponse(res, {
                    code: 200,
                    message: "删除成功"
                })
            } else {
                Response.errorresponse(res, {
                    code: 400,
                    msg: "删除失败"
                })
            }
        }
    } catch (error) {
        console.log(error);
        Response.errorresponse(res, {
            code: 500,
            msg: "服务器错误",
            err: error
        })
    }
}
/* 说说点赞 */
const addstrs = async (req, res) => {
    try {
        let { shuoshuo_id, status } = req.body
        let rese = await checkShuoshuo(shuoshuo_id)
        if (rese.code) {
            let ress = await setlikeshuoshuo(shuoshuo_id, status)
            if (ress.code) {
                Response.successresponse(res, {
                    code: 200,
                    message: "成功"
                })
            } else {
                Response.errorresponse(res, {
                    code: 400,
                    msg: "点赞失败"
                })
            }
        } else {
            Response.errorresponse(res, {
                code: 400,
                msg: "说说不存在"
            })
        }

    } catch (error) {
        console.log(error);
        Response.errorresponse(res, {
            code: 500,
            msg: "服务器错误",
            err: error
        })
    }
}
module.exports = {
    tovisible,
    updatatop,
    addshuoshuo,
    uploadshuoshuoimg,
    getshuoshuo,
    gettop,
    getshuoshuoByid,
    editshuoshuo,
    updatashuoshuoimg,
    updatacover,
    todelete,
    cantop,
    addstrs
}